/*
 * AssertionConsumerService
 * 
 * Version information
 * 
 * Apr 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.management.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.dataObject.LoginData;
import net.amsoft.iservice.isso.client.ISSOClient;
import net.amsoft.iservice.isso.client.SAMLDataObject;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.ResolverClient;
import org.apache.soap.Envelope;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;

/**
 * @author mohit
 * 
 */
public class AssertionConsumerService extends BaseAction {

    public ActionForward process(ActionMapping oMapping,
            ActionForm oActionForm, HttpServletRequest oRequest,
            HttpServletResponse oResponse) throws Exception {
        IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + F_ENTRY);
        ActionMessages oMsgs = new ActionErrors();
        LoginData oLoginData = (LoginData) oRequest.getSession(false)
                .getAttribute(IBrokerConstants.SESSION_LOGIN_OBJ);

        String sSAMLArtifactDoc = oRequest
                .getParameter(ISSOClient.PARAM_SAML_ART);

        String sRelayState = oRequest
                .getParameter(ISSOClient.PARAM_RELAY_STATE);

        SAMLDataObject oReceivedSAMLData = ISSOClient
                .getValuesFromSAMLArtifact(sSAMLArtifactDoc);
    
        if (!sRelayState.equals(oLoginData.getRelayState())
                || !oLoginData.getProviderIDHash().equals(
                        oReceivedSAMLData.getProviderIDHash())) {
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    IBrokerConstants.ERR_INVALID, "request"));
            saveErrors(oRequest.getSession(false), oMsgs);
            IServiceUtil.log(IServiceConstants.ERROR, this, F_PROCESS + 
                    "RS=" + oLoginData.getRelayState() + 
                    ",PIH=" + oLoginData.getProviderIDHash());
            IServiceUtil.log(IServiceConstants.ERROR, this, F_PROCESS + 
                    "received: RS=" + sRelayState + ",PIH=" + 
                    oReceivedSAMLData.getProviderIDHash());
            return oMapping.findForward(IBrokerConstants.FWD_SUCCESS);
        }

        String sArtifactConsumerService = ResolverClient.getMetaDataIDP(
                oLoginData.getIDPMetaDataEP())
                .getArtifactResolutionServiceURL();
        
        String sArtifactResolveXML = ISSOClient
                .createSAMLArtifactResolveRequest(oReceivedSAMLData
                        .getArtifact());

        Envelope oEnvelope = ISSOClient.createSOAPEnvelope(sArtifactResolveXML);

        // TODO TO BE REMOVED
        MessageResources resources = getResources(oRequest);
        String cert_path = resources.getMessage("cert.path");
        String keystore_pwd = resources.getMessage("cert.pwd");
        
        boolean isSecure = Boolean.parseBoolean(resources.getMessage("secure"));
        Envelope oReceivedEnvelope = null;
        if (isSecure) {
            oReceivedEnvelope = ISSOClient.sendAndReceiveSOAPEnvelope(
                    oEnvelope, sArtifactConsumerService);
        } else {
            oReceivedEnvelope = ISSOClient.sendAndReceiveSOAPEnvelope(
                    oEnvelope, sArtifactConsumerService, cert_path,
                    keystore_pwd);
        }
        IServiceUtil.log(IServiceConstants.DEBUG, this, F_PROCESS + 
                "soap envelope received: " + oReceivedEnvelope);
        String sArtifactResponseXML = ISSOClient
                .getArtifactResponseFromSOAPEnvelpe(oReceivedEnvelope);

        SAMLDataObject oSAMLAuthResponseData = ISSOClient
                .getAuthnResponseValuesFromArtifactResponse(sArtifactResponseXML);

        if (!oLoginData.getGlobalInumber().equals(
                oSAMLAuthResponseData.getSubjectNameIDNameQualifier())
                || !oSAMLAuthResponseData.getSAMLStatus().equals(
                        ISSOClient.SAML_STATUS_SUCCESS)) {
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    IBrokerConstants.ERR_INVALID, "request"));
            saveErrors(oRequest.getSession(false), oMsgs);
            IServiceUtil.log(IServiceConstants.WARN, this, F_PROCESS + "expected GIN:"
                    + oLoginData.getGlobalInumber() + ", received:"
                    + oSAMLAuthResponseData.getSubjectNameIDNameQualifier());
            IServiceUtil.log(IServiceConstants.WARN, this, F_PROCESS + "received STATUS:"
                    + oSAMLAuthResponseData.getSAMLStatus());
            IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + "redirect to=" + 
            		IBrokerConstants.FWD_SUCCESS);
            return oMapping.findForward(IBrokerConstants.FWD_SUCCESS);
        }
        oLoginData.setLoginStatus(true);
        oLoginData.setSAMLData(oSAMLAuthResponseData);
        oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                IBrokerConstants.MSG_LOGIN_SUCCESS));
        saveErrors(oRequest.getSession(false), oMsgs);

        IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + "redirect to=" + 
        		IBrokerConstants.FWD_SUCCESS);
        return oMapping.findForward(IBrokerConstants.FWD_SUCCESS);

    }

}
